package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.AlbumMapper;
import cn.tedu.csmall.product.pojo.dto.AlbumAddNewDto;
import cn.tedu.csmall.product.pojo.entity.Album;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.service.IAlbumService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class AlbumServiceImpl implements IAlbumService {

    @Autowired
    private AlbumMapper mapper;

    @Override
    public void addNew(AlbumAddNewDto albumAddNewDto) {
        log.debug("开始处理【添加相册】的业务，参数：{}", albumAddNewDto);
        String name=albumAddNewDto.getName();
        int i=mapper.countByName(name);
        log.debug("根据名称统计数量，结果：{}", i);
        if (i>0){
            String message="添加相册失败，相册名称已经被占用！";
            log.warn("添加相册失败，相册名称【{}】已经被占用！", name);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        Album album=new Album();
        BeanUtils.copyProperties(albumAddNewDto,album);
        log.debug("即将执行插入数据，参数：{}", album);
        mapper.insert(album);
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据ID删除相册】的业务，参数：{}", id);
        AlbumStandardVO albumStandardVO=mapper.getStandardById(id);
        if (albumStandardVO==null){
            String message="删除相册失败，相册名称已经被删除！";
            log.warn("删除相册失败，相册id【{}】不存在！", id);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        mapper.deleteById(id);
    }
}
